/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.base.radar;

import com.hitechbangla.fctu.base.AutoLockable;
import com.hitechbangla.fctu.base.Location;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.base.base.Base;
import com.hitechbangla.fctu.base.base.Kpi;
import com.hitechbangla.fctu.utility.GeneralUtil;
import fctu.simulator.HTB_MAIN;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author deb kanti
 */
public class AutoLockGround  implements Serializable
{
    private transient HTB_MAIN htb_main;
   
    private int phase; // phase 0 means no aircraft autolocked yet, 1 means 1 autolocked, and so on
    private final int range = 5;
    private Aircraft aircraft;
    private AutoLockable autoLockable;    
    
    private final int RECT_SIZE = 10;
    private Radar radar;
    private List<AutoLockGroundPair> listAutoLockGroundPair;
    
    public AutoLockGround(Radar radar)
    {
	//this.htb_main = htb_main;	
	phase = 0;
	this.radar = radar;
	this.radar.addAutoLockGround(this);
	listAutoLockGroundPair = new ArrayList<>();
    }
    
    public void execute(Point2D point)
    {	
	//System.out.println("from autolockground: phase " + getPhase());
	
	if(getPhase() == 0)
	{
	    checkIfAircraftClicked(point);
	}
	else if(getPhase() == 1)
	{
	    // now we have to see if we have hit any base, kpi etc
	    checkIfAutoLockableClicked(point);
	}
    }
    
    public void executeRemoveAutoLockGround(Point2D point)
    {
	checkIfAutoLockLineClicked(point);
    }
    
    private Detected getDetected(Aircraft aircraft)
    {
	for(Object objectDetected : this.radar.getDetectedMap().values())
	{
	    Detected detected = (Detected)objectDetected;
	    
	    if(detected.getAircraft().getAircraftState().getCallSign().equals(aircraft.getAircraftState().getCallSign()))
	    {
		return detected;
	    }
	}
	return null;
    }
    
    private AutoLockable getDetectedAutoLockable(AutoLockable autoLockable) 
    {
	if(autoLockable.getClass() == Base.class)
	{
	    Base baseCheck = (Base)autoLockable;
	
	    for(Base base: HTB_MAIN.gameScene.baseList)
	    {
		if(base.getBaseType().getName().equals(baseCheck.getBaseType().getName()))
		{
		    return base;
		}
	    }
	}
	
	if(autoLockable.getClass() == Kpi.class)
	{
	    Kpi kpiCheck = (Kpi)autoLockable;
	
	    for(Kpi kpi: HTB_MAIN.gameScene.kList)
	    {
		if(kpi.getName().equals(kpiCheck.getName()))
		{
		    return kpi;
		}
	    }
	}	
	
	return null;
    }
    
    private void checkIfAutoLockLineClicked(Point2D point)
    {
	Point2D point2 = new Point2D.Double();
	point2.setLocation(point.getX()*HTB_MAIN.one_nm_equivalent_pixel, point.getY()*HTB_MAIN.one_nm_equivalent_pixel);
	
	for(AutoLockGroundPair autoLockGroundPair:listAutoLockGroundPair) 
	{
	    Detected detectedFirst = getDetected(autoLockGroundPair.getAircraft());
	    AutoLockable autoLockable = getDetectedAutoLockable(autoLockGroundPair.getAutoLockable());	    
	    
	    
	    if(GeneralUtil.isPointCloseToLine((double)point2.getX(), (double) point2.getY(), (double)detectedFirst.getAcPosX(), (double)detectedFirst.getAcPosY(), (double)autoLockable.getLocation().getX(), (double)autoLockable.getLocation().getY()))
	    {
		listAutoLockGroundPair.remove(autoLockGroundPair);
	    }
	}
    }
    
    
    private void checkIfAutoLockableClicked(Point2D point)
    {
	//first testing the base 
	for(Base base: htb_main.gameScene.baseList)
	{
	    if(withinRangeAutoLockable(base, point))
	    {
		this.autoLockable = base;
		
		AutoLockGroundPair autoLockGroundPair = new AutoLockGroundPair(this.getAircraft(), this.autoLockable);
		getListAutoLockGroundPair().add(autoLockGroundPair);
		
		this.aircraft = null;
		this.autoLockable = null;
		
		phase = 0;
	    }
	}	
	//
	for(Kpi kpi: htb_main.gameScene.kList)
	{
	    if(withinRangeAutoLockable(kpi, point))
	    {
		this.autoLockable = kpi;
		
		AutoLockGroundPair autoLockGroundPair = new AutoLockGroundPair(this.getAircraft(), this.autoLockable);
		getListAutoLockGroundPair().add(autoLockGroundPair);
		
		this.aircraft = null;
		this.autoLockable = null;
		
		phase = 0;
	    }
	}
	
	if(phase !=0) // no base or kpi selected yet
	{
	    this.autoLockable = new AutoLockable(new Location((float)point.getX()*HTB_MAIN.one_nm_equivalent_pixel, (float)point.getY()*HTB_MAIN.one_nm_equivalent_pixel, 0));
	    AutoLockGroundPair autoLockGroundPair = new AutoLockGroundPair(this.getAircraft(), this.autoLockable);
	    getListAutoLockGroundPair().add(autoLockGroundPair);
		
	    this.aircraft = null;
	    this.autoLockable = null;
		
	    phase = 0;
	}
	
	
    }
    
    
    private void checkIfAircraftClicked(Point2D point)
    {
	//List<Aircraft> listAirCraft = HTB_MAIN.gameScene.airCraftList;
	
	for(Object object : this.radar.getDetectedMap().values())
	{
	   Detected detected = (Detected)object;
	   Aircraft aircraftTemp = detected.getAircraft();
	   
	   ////System.out.println("from autolock: aircraft detected");
	   
	   if(withinRange(aircraftTemp,point))
	    {					
		this.aircraft = aircraftTemp;
		phase = 1; // one aircraft selected
                
                double ax = detected.getAircraft().getAircraftState().getLocation().getX();
                double ay = detected.getAircraft().getAircraftState().getLocation().getY();
                double az = detected.getAircraft().getAircraftState().getLocation().getZ();
                
                
                
                Location loc = new Location(ax+1300,ay+1300,az);
                
                //detected.getAircraft().getAircraftState().setLocation(loc);
                double ax1 = detected.getAircraft().getAircraftState().getLocation().getX();
                double ay1 = detected.getAircraft().getAircraftState().getLocation().getY();
                
	    }	
           
	
	}	
    }

    
    // this method is same as AutoLock, we have to improvise the structure, like by polymorphism
    private boolean withinRange(Aircraft aircraft, Point2D point)
    {	
	Point2D point2 = new Point.Double();
	point2.setLocation(point.getX()*HTB_MAIN.one_nm_equivalent_pixel, point.getY()*HTB_MAIN.one_nm_equivalent_pixel);
	double distX = Math.abs((double)point2.getX() - aircraft.getAircraftState().getLocation().getX());
	double distY = Math.abs((double)(point2.getY() - aircraft.getAircraftState().getLocation().getY()));
	return (distX<range && distY <range);	
    }

    
    private boolean withinRangeAutoLockable(AutoLockable autoLockable, Point2D point)
    {	
	Point2D point2 = new Point2D.Double();
	point2.setLocation(point.getX()*HTB_MAIN.one_nm_equivalent_pixel, point.getY()*HTB_MAIN.one_nm_equivalent_pixel);
	double distX = Math.abs((double)point2.getX() - autoLockable.getLocation().getX());
	double distY = Math.abs((double)(point2.getY() - autoLockable.getLocation().getY()));
	return (distX<range && distY <range);	
    }

    /**
     * @return the listAutoLockGroundPair
     */
    public List<AutoLockGroundPair> getListAutoLockGroundPair() {
	return listAutoLockGroundPair;
    }

    /**
     * @return the phase
     */
    public int getPhase() {
	return phase;
    }

    /**
     * @return the aircraft
     */
    public Aircraft getAircraft() {
	return aircraft;
    }
    
    public void draw(Graphics g)
    {	
	Graphics2D g2d = (Graphics2D)g;
	
	if (getPhase() == 1) 
	{
	    for (Object objectDetected : this.radar.getDetectedMap().values()) 
	    {
		Detected detected = (Detected) objectDetected;

		if (detected.getAircraft().getAircraftState().getCallSign().equals(getAircraft().getAircraftState().getCallSign())) 
		{
		    g.setColor(Color.RED);
		    g2d.drawRect((int) (detected.getAcPosX() * HTB_MAIN.one_px_equivalent_naut) - 5, (int) (detected.getAcPosY() * HTB_MAIN.one_px_equivalent_naut) - 4, 10, 10);
		}
	    }
	}


	Detected detectedAircraft = null;

	for (AutoLockGroundPair autoLockGroundPair : getListAutoLockGroundPair()) 
	{
	    Aircraft aircraft = autoLockGroundPair.getAircraft();
	    AutoLockable autoLockable = autoLockGroundPair.getAutoLockable();

	    if (this.radar.isOutOfRange(aircraft)) 
	    {
		getListAutoLockGroundPair().remove(autoLockGroundPair);
	    } 
	    else 
	    {
		// drawing the aircraft
		for (Object objectDetected : this.radar.getDetectedMap().values()) 
		{
		    Detected detected = (Detected) objectDetected;

		    if (detected.getAircraft().getAircraftState().getCallSign().equals(aircraft.getAircraftState().getCallSign())) 
		    {
			g.setColor(Color.RED);
			g2d.drawRect((int) (detected.getAcPosX() * HTB_MAIN.one_px_equivalent_naut) - 5, (int) (detected.getAcPosY() * HTB_MAIN.one_px_equivalent_naut) - 4, 10, 10);
			detectedAircraft = detected;
			break;
		    }
		}
		// drawing the line between aircraft and autolockable

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.drawLine((int) (detectedAircraft.getAcPosX() * HTB_MAIN.one_px_equivalent_naut), (int) (detectedAircraft.getAcPosY() * HTB_MAIN.one_px_equivalent_naut), (int) (autoLockable.getLocation().getX() * HTB_MAIN.one_px_equivalent_naut), (int) (autoLockable.getLocation().getY() * HTB_MAIN.one_px_equivalent_naut));
                
                // draw to new loc autoLockable.getLocation().getX() * HTB_MAIN.one_px_equivalent_naut)
                //Location loc = new Location(ax+300,ay+300,az);
                double ax = autoLockable.getLocation().getX() ; //* HTB_MAIN.one_px_equivalent_naut
                double ay = autoLockable.getLocation().getY();
                double az = autoLockable.getLocation().getZ();
                Location loc = new Location(ax,ay,az);
                //detectedAircraft.getAircraft().getAircraftState().setLocation(loc);
                //detected.getAircraft().getAircraftState().setLocation(loc);

		double distance = (double) GeneralUtil.distance2D(detectedAircraft.getAcPosX(), detectedAircraft.getAcPosY(), autoLockable.getLocation().getX(), autoLockable.getLocation().getY());
		int stringPositionX = (int) ((detectedAircraft.getAcPosX() + autoLockable.getLocation().getX()) * HTB_MAIN.one_px_equivalent_naut / 2) - 10;
		int stringPositionY = (int) ((detectedAircraft.getAcPosY() + autoLockable.getLocation().getY()) * HTB_MAIN.one_px_equivalent_naut / 2) + 5;

		// drawString(,);
		HTB_MAIN.customDrawString(g, "distance:" + String.valueOf((int) distance), new Point(stringPositionX, stringPositionY));

		double heading = GeneralUtil.getHeadingWithOther(detectedAircraft.getAcPosX(), detectedAircraft.getAcPosY(), autoLockable.getLocation().getX(), autoLockable.getLocation().getY());
		HTB_MAIN.customDrawString(g, "heading:" + String.valueOf((int) heading), new Point(stringPositionX, stringPositionY + 12));
	    }
	}
    }

    
    
}
